package api;

import Dao.Blog;
import Dao.BlogDao;
import Dao.User;
import Dao.UserDao;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-11-02
 * Time: 21:11
 */
//通过这个类来加载用户的信息
@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过判断请求是否带有blogId这个参数，来判断是列表页发来的请求还是详情页发来的请求
        String blogId=req.getParameter("blogId");
        if(blogId==null){
            //表明是列表页
            //列表页可以从保存下来的session中拿到用户信息
            HttpSession session=req.getSession(false);
            //拿到的这个session可能为空
            if(session==null){
                //session为空，那么用户信息也必然为空，这里需要创建一个卡用户出来
                User user=new User();
                //Jackson ObjectMapper类(com.fasterxml.jackson.databind.ObjectMapper)解析JSON工具
                //
                //序列化：      Java对象生成Json
                //
                //反序列化：  Json解析为Java对象
                String respJson=objectMapper.writeValueAsString(user);
                resp.setContentType("application/json;charset=utf8");
                resp.getWriter().write(respJson);
                return;
            }
            User user= (User) session.getAttribute("user");
            if(user==null){
                user=new User();
                String respJson=objectMapper.writeValueAsString(user);
                resp.setContentType("application/json;charset=utf8");
                resp.getWriter().write(respJson);
                return;
            }
            String respJson=objectMapper.writeValueAsString(user);
            resp.setContentType("applicaion/json;charset=utf8");
            resp.getWriter().write(respJson);
        }else{
            //表明是详情页
            //需要查询数据库,通过blogId关联找到blog,再通过getuserid找到userid，通过userid找到user
            BlogDao blogDao=new BlogDao();
            Blog blog=blogDao.getBlog(Integer.parseInt(blogId));
            if(blog==null){
                User user=new User();
                String respJson=objectMapper.writeValueAsString(user);
                resp.setContentType("application/json;charset=utf8");
                resp.getWriter().write(respJson);
                return;
            }
            UserDao userDao=new UserDao();
            User user=userDao.getUserById(blog.getBlogId());
            if(user==null){
                user=new User();
                String respJson=objectMapper.writeValueAsString(user);
                resp.setContentType("application/json;charset=utf8");
                resp.getWriter().write(respJson);
                return;
            }
            String respJson=objectMapper.writeValueAsString(user);
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write(respJson);
        }
    }
}
